// 我的组队

Page({

    data: {
        background: ["#FAD7DB", "#FAD9B3", "#FCFDB1", "#CDFFFF", "#C4FDC6", "#FAD7DB", "#FAD9B3", "#FCFDB1", "#CDFFFF", "#C4FDC6"],
        list: '',
        sessionID: ''
    },
    myShowModal() {
        wx.showModal({
            title: '提示',
            content: '登录后查看更多，是否登录？',
            success: (res) => {
                // 确定
                if (res.confirm) {
                    this.logIn();
                }
                else {
                     wx.showToast({
                         title: '跳转到首页',
                         icon: 'none',
                        duration: 1500
                     })
                     setTimeout(function () {
                      wx.switchTab({
                        url: '/pages/recruit/recruit',
                        success:function(e)
                        {
                          var page = getCurrentPages().pop();
                          if (page == undefined || page == null) return;
                          page.onLoad();
                        }
                      })  
                    }, 1500)
                }
            }
        })
      },
    
      logIn() {
        // 如果userInfo为空，则未确认过微信授权弹窗，即首次登录
        if (!this.data.userInfo) {
            // 微信授权弹窗
            wx.getUserProfile({
                desc: '用于获取用户头像和昵称',
                // 用户点击确认
                success: (res) => {
                    let userInfo = res.userInfo;
                    wx.setStorageSync('userInfo', userInfo); // 缓存userInfo
                    this.myLogin(); // 进行登录
                },
                // 用户点击取消
                fail: (res) => {
                    // wx.showToast({
                    //     title: '跳转到首页',
                    //     icon: 'none',
                    //     duration: 1500
                    // })
                }
            })
        }
        // 非首次登录
        else {
            this.myLogin(); // 重新登录
        }
    },
    
    // wx.login()封装
    myLogin() {
        wx.login({
            success: (res) => {
                wx.request({
                    url: 'https://www.hschuangsai.com/user/login/' + res.code,
                    method: 'GET',
                    success: (res) => {
                        console.log("myLogin success", res);
                        let sessionID = res.data.data.sessionId;
                        wx.setStorageSync('chuangNum',res.data.data.chuangNum)
                        wx.setStorageSync('sessionID', sessionID); // 缓存sessionID
                        wx.setStorageSync('loginFlag', 1); // 缓存loginFlag
                        this.setData({
                            sessionID: sessionID,
                            loginFlag: 1,
                        });
    
                        wx.showToast({
                            title: '成功登录，继续创建队伍',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                })
            }
        })
    },
    onLoad() {
        this.setData({
            userInfo: wx.getStorageSync('userInfo'), // 用户的微信头像、昵称等信息
            loginFlag: wx.getStorageSync('loginFlag'), // 判断是否登录，登录为1，未登录为0或空
            sessionID: wx.getStorageSync('sessionID'),
            chuangNum: wx.getStorageSync('chuangNum'),
          })
        if (this.data.loginFlag != 1) {
            this.myShowModal();
        }

        // 获取后端数据，显示组队列表
        wx.request({
            url: 'https://www.hschuangsai.com/user/team',
            method: 'POST',
            data: {
                pageParam: {
                    pageSize: 1000,
                    pageNum: 1,
                    orderBy: "id asc"
                },
                TeamType: 2
            },
            header: {
                'session': this.data.sessionID
            },
            success: (res) => {
                if (res.statusCode!=200) {
                    wx.showToast({
                        title: '获取列表失败',
                        icon: 'none',
                        duration: 800     
                      })  
                }
                let list = res.data.data.list;
                console.log("list",list)

                for (let i of list) {
                    if (i.status === 0) {
                        i.state = '进行中';
                    }
                    else {
                        i.state = '已结束';
                    }
                    i.tags = new Array();
                    i.tags[0] = i.tag1;
                    i.tags[1] = i.tag2;
                    i.tags[2] = i.tag3;
                }
                this.setData({
                    list: list
                })
            }
        });
    }
})